package com.tqw.item.controller;

import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.item.service.FavoritesService;
import com.tqw.item.service.ItemDescService;
import com.tqw.item.service.ItemService;
import com.tqw.pojo.Client;
import com.tqw.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.tqw.entity.StatusCode.*;

/**
 * @description: some desc
 * @author: Administrator
 * @date: 2022/4/11 10:17
 */
@RestController
//ajax调用,使用网关要注释
//@CrossOrigin
@RequestMapping("/item/itemDesc")
public class ItemDescController {
    @Autowired
    private ItemDescService itemDescService;
    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private ItemService itemService;

    /**
     * 商品详情页面显示
     * 请求类型get
     * 请求路径 URL：http://localhost:8080/#/item/itemDesc/{id}/{token}
     * 返回参数SysResult
     * @return
     */
    @GetMapping("")
    public Result itemDesc(HttpServletRequest request,Integer itemId) {
        String token = request.getHeader("Authorization");
        Date date = new Date();
        Map<String, Object> map = itemDescService.findItemDesc(itemId);
        if (map == null) {
            return new Result<String>(false, ERROR, "商品已出售或下架");
        }
        //收藏夹按钮
        try {
            if (!JWTUtil.isExpired(token)) {
                Item item = (Item) map.get("item");
                String favorites = favoritesService.findfavorites(token, item.getId());
                map.put("favoritese", favorites);
            } else {
                map.put("favoritese", "添加收藏夹");
            }
        } catch (Exception e) {
            map.put("favoritese", "添加收藏夹");
        }
        Date date1 = new Date();
        System.out.println("所用时间" + (date1.getTime() - date.getTime()));
        return new Result<Map<String, String>>(true, OK, "查询成功", map);
    }

    /**
     * 修改商品详情缓存
     * 请求类型get
     * 请求路径 URL：http://localhost:8080/#/item/itemDesc/update?itemId=?&status=?
     * 返回参数 无
     * @return
     */
    @GetMapping("/updateRedis")
    public void updateItemDesc(Integer itemId,Character status){
        itemDescService.updateItemDesc(itemId,status);
    }

    /**
     * 跳转到购买页面
     * 请求类型post
     * 请求路径 URL：http://localhost:8080/#/item/itemDesc/findToken/{token}
     * 返回参数Result
     * @param token
     * @return
     */
    @PostMapping("/findToken/{id}")
    public Result findToken(@RequestHeader("Authorization") String token,@PathVariable Integer id){
        Integer code=itemDescService.findToken(token,id);
        if (code==null){
            return new Result(false, ERROR, "该商品已售出或下架");
        }else if (code==205){
            return new Result(false, REPERROR, "该商品为本人商品");
        }else if (code==206){
            return new Result(false, TOKEN_EXPIRED, "token过期");
        }
        return new Result();
    }
}
